package com.wt.struggle.goodbye

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.support.v4.app.ActivityCompat
import android.view.KeyEvent
import android.view.View
import com.wt.struggle.goodbye.base.Config
import com.wt.struggle.goodbye.base.ProActivity
import com.wt.struggle.goodbye.base.Send
import com.wt.struggle.goodbye.base.Share
import com.wt.struggle.goodbye.info.NetInfo
import com.wt.struggle.goodbye.main.activity.OnToListener
import com.xin.lv.yang.utils.utils.StatusBarUtil
import kotlinx.android.synthetic.main.main_load_layout.*
import org.json.JSONObject
import java.util.*

/**
 * 引导页面
 */
class LoadActivity : ProActivity() {

    override fun handler(what: Int, arg1: Int, netInfo: NetInfo) {

        when (what) {
            Send.START_CODE -> {
                if (netInfo.code == Config.SUCCESS) {

                    val result = netInfo.dataResult

                    if (result != null && result != "" && result != "null") {

                        val jsonObject = JSONObject(result)

                        if (jsonObject != null) {

                            val http = jsonObject.optString("domainname")
                            val path = jsonObject.optString("path")

                            val arr = arrayListOf<String>(http + path.subSequence(1, path.length))

                            initViewPager(loadViewPager, arr, object : OnToListener {
                                override fun onToListener() {
                                    startActivity(Intent(this@LoadActivity, MainActivity::class.java))
                                    if (timer != null) {
                                        timer!!.cancel()
                                    }
                                    finish()
                                }
                            })

                            time()
                        }

                    } else {

                        initViewPager(loadViewPager, arrayListOf(), object : OnToListener {
                            override fun onToListener() {

                            }
                        })
                    }

                }
            }

            1212 -> {

                log("执行到此信息-------")

                getLoad()

            }

            555 -> {
                tvTime.text = timeNum.toString()

                if (timeNum == 0) {

                    startActivity(Intent(this@LoadActivity, MainActivity::class.java))
                    timer!!.cancel()

                    finish()
                }
            }

        }

    }

    var timer: Timer? = null

    var timeNum = 3


    fun time() {

        timer = Timer()

        button.visibility = View.GONE

        tvTime.visibility = View.GONE

        tvTime.text = timeNum.toString()

        val timerTask = object : TimerTask() {
            override fun run() {
                timeNum--
                val message = handler!!.obtainMessage()
                message.arg1 = timeNum
                message.obj = ""
                message.what = 555
                handler!!.sendMessage(message)
            }
        }

        timer!!.schedule(timerTask, 0, 1000)

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_load_layout)

        getLoad()

        button.setOnClickListener {
            startActivity(Intent(this, MainActivity::class.java))
            if (timer != null) {
                timer!!.cancel()
            }
            finish()
        }

        val isTui = Share.getTui(this)

        if (isTui) {
            /// 绑定推送
        } else {
            /// 解除绑定推送
        }
    }

    /**
     * 获取引导页图片，需要获取手机获取 imei 权限
     */
    private fun getLoad() {

        val code = ActivityCompat.checkSelfPermission(this, android.Manifest.permission.READ_PHONE_STATE)
        if (code == PackageManager.PERMISSION_GRANTED) {

            send!!.getMainLoad(handler)

        } else {
            ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_PHONE_STATE), 888)
        }

    }


    /**
     * 获取请求的sessionid
     */
    private fun getSession() {
        val code = ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)
        if (code == PackageManager.PERMISSION_GRANTED) {
            send!!.getSession(handler)
        } else {
            ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_PHONE_STATE), 3456)
        }
    }


    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)

        if (requestCode == 3456 &&
            grantResults[0] == PackageManager.PERMISSION_GRANTED
        ) {
            send!!.getSession(handler)
        }

        if (requestCode == 888) {

            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                send!!.getMainLoad(handler)

            } else {
                showToastShort("请检查获取手机状态权限")
                handler!!.postDelayed(Runnable {
                    finish()
                }, 2000)

            }
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK && event!!.action == KeyEvent.ACTION_DOWN) {
            if (timer != null) {
                timer!!.cancel()
            }

            finish()
        }

        return super.onKeyDown(keyCode, event)
    }

}